package cn.zust.itcost.utils;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Column;

import java.util.*;

/**
 * 运行codegenerator代码生成器，生成相应的代码
 * 不用在意，创建软件包用的
 */
public class CodeGenerator {
    // 基础信息配置
    /**
     * 数据库连接字符
     */
    private static final String URL = "jdbc:mysql://47.109.136.192:3306/resume?serverTimezone=GMT%2B8&characterEncoding=UTF-8";

    private static final String PARENT_PACKAGE_NAME = "cn.zust.itcost";

    /**
     * 数据库用户名
     */
    private static final String USERNAME = "resume";
    /**
     * 数据库密码
     */
    private static final String PASSWORD = "123456";
    /**
     * 项目根路径
     */
    private static final String PROJECT_ROOT_PATH = System.getProperty("user.dir");

    /**
     * 执行此处
     */
    public static void main(String[] args) {
        simpleGenerator();
    }

    /**
     * 【单模块】简单的实现方案
     */
    protected static void simpleGenerator() {

        // 包路径
        String packagePath = PROJECT_ROOT_PATH + "/src/main/java";
        // XML文件的路径
        String mapperXmlPath = PROJECT_ROOT_PATH + "/src/main/resources/mapper";

        // 开始执行代码生成
        FastAutoGenerator.create(URL, USERNAME, PASSWORD)
                .globalConfig(builder -> builder
                        .author("YXZhou")
                        .fileOverride()
                        .disableOpenDir()
                        .outputDir(packagePath)
                        .dateType(DateType.TIME_PACK)
                        .commentDate("yyyy-MM-dd")
                )
                .packageConfig((scanner, builder) -> builder
                        .parent(PARENT_PACKAGE_NAME)
                        //.moduleName(scanner.apply("请输入模块名："))
                        //.pathInfo(Collections.singletonMap(OutputFile.mapperXml, mapperXmlPath))
                )
                .strategyConfig((scanner, builder) -> builder.addInclude(getTables(scanner.apply("请输入表名，多个英文逗号分隔？生成所有表，请输入[all]：")))
                        .entityBuilder()
                        .enableTableFieldAnnotation()
                        .logicDeleteColumnName("is_delete")
                        .logicDeletePropertyName("isDelete")
                        .addTableFills(new Column("create_time", FieldFill.INSERT))
                        .addTableFills(new Column("update_time", FieldFill.INSERT_UPDATE))
                        .mapperBuilder()
                        .enableMapperAnnotation()
                        .enableBaseResultMap()
                        .enableBaseColumnList()
                        .controllerBuilder()
                        .enableRestStyle()
                        .enableHyphenStyle()
                        .build()
                )
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();
    }


    // 处理 all 情况
    protected static List<String> getTables(String tables) {
        return "all".equals(tables) ? Collections.emptyList() : Arrays.asList(tables.split(","));
    }

    /**
     * <p>
     * 读取控制台内容
     * </p>
     */
    private static String scanner() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入" + "请输入模块名：" + "：");
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNotBlank(ipt)) {
                return ipt;
            }
        }
        throw new MybatisPlusException("请输入正确的" + "请输入模块名：" + "！");
    }

}
